package com.yq.day30.udp1.v4;

import com.yq.utils.NetUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/30 17:35
 */
public class SendTask implements Runnable {

    DatagramSocket socket;
    String ip;
    int port;

    public SendTask(DatagramSocket socket, String ip, int port) {
        this.socket = socket;
        this.ip = ip;
        this.port = port;
    }
    @Override
    public void run() {
        Scanner scanner = new Scanner(System.in);

        //只发消息
        while (true) {
            String s = scanner.nextLine();

            //把数据封装
            try {
                DatagramPacket sendPacket = NetUtils.getSendPacket(s, ip, port);
                socket.send(sendPacket);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //send

        }
    }
}
